import 'package:ac/api/ac_urls.dart';
import 'package:ac/common/ac_pv_manager.dart';
import 'package:ac/data/ac_app_preferences.dart';
import 'package:ac/data/ac_user_manager.dart';
import 'package:ac/route/ac_route_pages.dart';
import 'package:get/get.dart';

import 'ac_splash_state.dart';

class AcSplashLogic extends GetxController {
  final AcSplashState state = AcSplashState();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    _checkGuide();
  }

  void _checkGuide() async {
    await Future.delayed(const Duration(seconds: 1));
    await AcUserManager.init();
    LogPvManger.stat(page: LogPages.launchPage);
    if (AcAppPreferences.instance.getGuideFlag()) {
      var token = AcUserManager.userInfoModel?.token;
      if (token != null) {
        var result = await AcUrls.isValidToken.post(
            showLoading: false,
            showErrorMessage: false,
            data: {"token": "Bearer$token"});
        if (result?["code"] == 0) {
          Get.offNamed(AcRouteNames.home);
        } else {
          Get.offNamed(AcRouteNames.login);
        }
        return;
      }
      if (AcUserManager.userInfoModel?.token == null) {
        Get.offNamed(AcRouteNames.login);
      } else {
        Get.offNamed(AcRouteNames.home);
      }
    } else {
      Get.offNamed(AcRouteNames.guide);
    }
  }
}
